package com.project.user.controller.controller;

import com.project.user.entity.UserInfoExample;
import com.project.user.entity.UserInformationViewExample;
import com.project.user.entity.entitly.UserInfo;
import com.project.user.entity.entitly.UserInformationView;
import com.project.user.mapper.UserInfoMapper;
import com.project.user.mapper.UserInformationViewMapper;
import com.untils.IdWorker;
import com.untils.MybatisClass;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.Map;

public class UserInfoController implements com.project.user.controller.UserInfoController {
    static IdWorker worker = new IdWorker(1, 1, 1);

    @Override
    public UserInformationView UserInfo(int userInfoId, String phone, String username) throws Exception {

        UserInformationView userInformationViews = new UserInformationView();
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UserInformationViewExample userInformationViewExample = new UserInformationViewExample();
        UserInformationViewMapper userInfoMapper = sqlSession.getMapper(UserInformationViewMapper.class);

        if (userInfoId>=0) {
            userInformationViewExample.createCriteria().andIdIsNotNull().andIdEqualTo(userInfoId);
        } else if (phone != null) {
            userInformationViewExample.createCriteria().andIdIsNotNull().andUserPhonenumberLike(phone);
        } else if (username != null) {
            userInformationViewExample.createCriteria().andIdIsNotNull().andUserNameLike(username);
        } else {
            userInformationViewExample.createCriteria().andIdIsNotNull().andUserPhonenumberEqualTo(phone).andIdEqualTo(userInfoId);
        }

        try {
            userInformationViews = userInfoMapper.selectByExample(userInformationViewExample).get(0);
        } finally {
            sqlSession.close();
        }

        return userInformationViews;
    }

    @Override
    public Map<String, Object> UserInsert(String password, String phone) throws Exception {
        Map<String, Object> userSession = new HashMap<>();
        /*create a user info*/
        UserInfo userInfo = new UserInfo(String.valueOf(worker.nextId()), "user" + phone, password, phone);

        /*insert a new user for table userinfoTable */
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
        int UserInfoSuccess = -1;

        try {
            UserInfoSuccess = userInfoMapper.insert(userInfo);
        } finally {
            sqlSession.close();
        }

        userSession.put("is-successfully",UserInfoSuccess);
        userSession.put("user-id",userInfo.getUserId());
        return userSession;
    }

    @Override
    public UserInfo UserLogin(String password, String phone) throws Exception {

        SqlSession sqlSession = MybatisClass.getSqlSession();
        UserInfo userInfo = new UserInfo();
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfoExample userExample = new UserInfoExample();

        try {
            userExample.createCriteria().andIdIsNotNull().andUserPhonenumberEqualTo(phone).andUserPasswordEqualTo(password);
            userInfo = userInfoMapper.selectByExample(userExample).get(0);
        } finally {
            sqlSession.close();
        }

        return userInfo;
    }

    @Override
    public int updateUserInfo(UserInfo userInfo) {
        SqlSession sqlSession = MybatisClass.getSqlSession();
        int successfully = -1;
        UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfoExample userExample = new UserInfoExample();
        try {
            successfully = userInfoMapper.updateByPrimaryKey(userInfo);
        } finally {
            sqlSession.close();
        }
        return successfully;
    }
}
